# predicting.competence.R
#
# Predict candidate facial competence with district competitiveness
# and other covariates.

cat('\nRunning candidate face models ...\n')

# ---------------------------------------------------
# Get Data
# ---------------------------------------------------
house <- read.csv("house.csv",as.is=T)
senate <- read.csv("senate.csv",as.is=T)

# Subset Senate and reshape consistent with house.
senate <- senate[senate$year>1990,]         # Only have Cook for 1992+

senate <- senate[,c("state","year","votes","incumbent",
                    "party","cook","comp","Disbursements")]
senate <- reshape(senate,v.names=c("votes","incumbent","comp","Disbursements"),
                idvar=c("state","year"),
                timevar="party",direction="wide")
senate$dem_pctvote <- with(senate,votes.dem/(votes.dem + votes.rep))*100
names(senate)[names(senate)=="incumbent.dem"] <- "dem.incum"
names(senate)[names(senate)=="incumbent.rep"] <- "rep.incum"

# ---------------------------------------------------
# Create incumbent-centered variables.
# ---------------------------------------------------

# Challenger/incumbent facial competence.
house$chall.face <- ifelse(house$dem.incum==1,house$rep_comp.legacy,
                    ifelse(house$rep.incum==1,house$dem_comp.legacy,
                    NA)) # Keep open seats as NA for now.
house$incum.face <- ifelse(house$dem.incum==1,house$dem_comp.legacy,
                    ifelse(house$rep.incum==1,house$rep_comp.legacy,
                    NA)) # Keep open seats as NA for now.
senate$chall.face <- ifelse(senate$dem.incum==1,senate$comp.rep,
                    ifelse(senate$rep.incum==1,senate$comp.dem,
                    NA)) # Keep open seats as NA for now.
senate$incum.face <- ifelse(senate$dem.incum==1,senate$comp.dem,
                    ifelse(senate$rep.incum==1,senate$comp.rep,
                    NA)) # Keep open seats as NA for now.

# Collapse Cook score and code towards incumbency
# 1=SafeChallenger, 2=LikelyChallenger, 3=LeanChallenger, 4=TossupChallenger
# 5=TossupIncumbent, 6=LeanIncumbent, 7=LikelyIncumbent, 8=SafeIncumbent

senate$cook.numeric <- with(senate,ifelse(cook=="SolidDem",1,
                                ifelse(cook=="LikelyDem",2,
                                ifelse(cook=="LeanDem",3,
                                ifelse(cook=="TossupDem"|cook=="TossUpDem",4,
                                ifelse(cook=="TossupRep"|cook=="TossUpRep",5,
                                ifelse(cook=="LeanRep",6,
                                ifelse(cook=="LikelyRep",7,
                                ifelse(cook=="SolidRep",8,NA)))))))))
house$cook.numeric <- with(house,ifelse(cook=="LikelyDem",2,
                                ifelse(cook=="LeanDem",3,
                                ifelse(cook=="TossupDem"|cook=="TossUpDem",4,
                                ifelse(cook=="TossupRep"|cook=="TossUpRep",5,
                                ifelse(cook=="LeanRep",6,
                                ifelse(cook=="LikelyRep",7,
                                ifelse(dem.incum==1&cook=="Safe",1,
                                ifelse(rep.incum==1&cook=="Safe",8,
                                NA)))))))))

# Code away from incumbent by accounting for partisanship
# Higher values of cook.collapse mean more risk for
# the incumbent
senate$cook.collapse <- with(senate,
                        ifelse(rep.incum==1,9-cook.numeric,
                        ifelse(dem.incum==1,cook.numeric,NA)))
senate$cook.collapse <- senate$cook.collapse - 4    # Center

house$cook.collapse <-  with(house,
                        ifelse(rep.incum==1,9-cook.numeric,
                        ifelse(dem.incum==1,cook.numeric,NA)))
house$cook.collapse <- house$cook.collapse - 4    # Center

# Save out tex file of regressions
source("lmLatexWriter.R")
   
listOfLms <- list()
# House is 2004 only, Senate is 1992-2006
listOfLms[["House Challengers"]] <- lm(chall.face ~ cook.collapse,
            data=house)

listOfLms[["Senate Challengers"]] <- lm(chall.face ~ cook.collapse
            + as.factor(year),
            subset=senate$cook.collapse<1,
            data=senate)

listOfLms[["House Incumbents"]] <- lm(incum.face ~ cook.collapse,
            data=house)

listOfLms[["Senate Incumbents"]] <- lm(incum.face ~ cook.collapse
            + as.factor(year),
            subset=senate$cook.collapse<1,
            data=senate)


varlabs <- list(chall.face="Challenger Facial Competence",
            incum.face="Incumbent Facial Competence",
            cook.collapse="Cook Incumbent Risk",
            chall.opposed="Challenger Selected in Contested Primary")
varlabs[['as.factor(cook.collapse)4']] <- "Cook: Incumbent Safe"
varlabs[['as.factor(cook.collapse)3']] <- "Cook: Incumbent Likely"
varlabs[['as.factor(cook.collapse)2']] <- "Cook: Incumbent Lean"
for ( year in paste(unique(senate$year)) ) {
    varlabs[[paste('as.factor(year)',year,sep='')]] <- paste(year,"Fixed Effect")
}
statsLs=list(r.squared="$R^2$",adj.r.squared="Adjusted $R^2$",
            sigma="Std. Error of Regression")
lmLatexWriter(listOfLms,toFile=F,rounder="%7.3f",
            varlabs=varlabs,statsLs=statsLs)
lmLatexWriter(listOfLms,toFile=T,rounder="%7.3f",
            varlabs=varlabs,statsLs=statsLs,
            outfile="TableAndFiguresOutput/Table_2.tex")
